/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.web.controller;

import com.bifrost.entity.CountryArea;
import com.bifrost.model.AreaModel;
import com.bifrost.common.model.ResultModel;
import com.bifrost.service.CountryAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bifrost
 * @version 1.0.0
 * @Date 2017/10/17 13:41
 */
@RestController("apiAreaController")
@RequestMapping("/api/city")
@Api("地区相关接口")
public class AreaController {
    @Autowired
    CountryAreaService countryAreaService;

    @GetMapping("/all")
    @ApiOperation(value = "获取所有二级城市地区")
    public ResultModel getAreas() {
        List<CountryArea> areas = countryAreaService.getAllCities();
        if (areas == null) {
            return ResultModel.error("没有满足条件的数据");
        }
        Map<String, List> result = new HashMap(26);
        for (CountryArea area : areas) {
            String initial = area.getInitial();
            AreaModel model = new AreaModel(area.getId(), area.getName());
            model.setInitial(initial);
            model.setLatitude(area.getLatitude());
            model.setLongitude(area.getLongitude());
            model.setDepth(area.getDepth());
            model.setZipcode(area.getZipcode());
            if (result.containsKey(initial)) {
                result.get(initial).add(model);
            } else {
                List list = new ArrayList();
                list.add(model);
                result.put(initial, list);
            }
        }
        return ResultModel.ok().put("areas", result);
    }

    @GetMapping("/areas")
    @ApiOperation(value = "按照级联形式获取所有城市地区")
    public ResultModel allAreas() {

        List<Map> tree = countryAreaService.getAreasTree();
        return ResultModel.ok("成功").put("areas", tree);

    }
}
